/*
 * Copyright (c) 2021, Texas Instruments Incorporated
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * *  Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * *  Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * *  Neither the name of Texas Instruments Incorporated nor the names of
 *    its contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "ti_msp_dl_config.h"
#include "key.h"
#include "upper_community.h"
#include "Emm_V5.h"
#include "lcd.h"
#include "page_func.h"


#define delay_ms(ms) delay_cycles(CPUCLK_FREQ/1000 * ms)

#define pixel_2_reality 0.511f
#define Angle_2_Pulse	8.889f
#define Distance 		820.0f
#define PI 				3.1415926f
#define Alpha			0.95

RxContext community_rx_buffer = {0};
ring_buffer_t rx_fifo = {0};
extern float pitch_set,yaw_set;


int main(void)
{
    SYSCFG_DL_init();
	
	NVIC_ClearPendingIRQ(ZDT_Com_INST_INT_IRQN);
	NVIC_EnableIRQ(ZDT_Com_INST_INT_IRQN);
	
	NVIC_ClearPendingIRQ(K230_Com_INST_INT_IRQN);
	NVIC_EnableIRQ(K230_Com_INST_INT_IRQN);
	
	//initialize the community with K230
	com_rx_context_init(&community_rx_buffer);
	com_ring_buffer_init(&rx_fifo);
	//initialize the button scan
	user_button_init();
	//initialize the LCD driver
	//lcd_init();
	//page_init();
	//the ZDT step motor : 3200 Pulses is 1 circle
    while (1) 
			{
				com_process_rx_data(&rx_fifo,&community_rx_buffer);
				//page_change();
				delay_ms(5);
			}
}


double yaw_real = 0.0f,pitch_real = 0.0f;

void motor_sync_move(void)
{
	float yaw_pulse_temp = 0.0f,pitch_pulse_temp= 0.0f;
	double k= 0.0f;
	
	yaw_real = my_atan2(yaw_set*pixel_2_reality,Distance)*180.0f/PI;
	pitch_real 	= my_atan2(pitch_set*pixel_2_reality,Distance)*180.0f/PI;

	yaw_pulse_temp = yaw_real * Angle_2_Pulse*Alpha;
	pitch_pulse_temp = pitch_real * Angle_2_Pulse*Alpha;
	
	k = fabsf((float)(pitch_real/yaw_real));
	
	if(yaw_real <0)
	{
		yaw_pulse_temp = - yaw_pulse_temp;
		uint16_t yaw_pulse = yaw_pulse_temp*1;
		Emm_V5_Pos_Control(1,0,10,0,yaw_pulse,0,1);
		delay_ms(1);
	}
	else
	{

		uint16_t yaw_pulse = yaw_pulse_temp*1;
		Emm_V5_Pos_Control(1,1,10,0,yaw_pulse,0,1);
		delay_ms(1);
	}
	
	//yaw_set = 0;
	
	if(pitch_real <0)
	{
		pitch_pulse_temp = -pitch_pulse_temp;
		uint16_t pitch_pulse = pitch_pulse_temp*1;
		Emm_V5_Pos_Control(2,0,10.0f*k,0,pitch_pulse,0,1);
		delay_ms(1);
	}
	else
	{

		uint16_t pitch_pulse = pitch_pulse_temp*1;
		Emm_V5_Pos_Control(2,1,10.0f*k,0,pitch_pulse,0,1);
		delay_ms(1);
	}
	
	//pitch_set = 0;
	
	Emm_V5_Synchronous_motion(0);
	delay_ms(1);

}
